<?
include("../include/database.php");

/*
Module: sessie
Version: 1.0
Writed by: Barth Steeg


This module makes a session and with function 'check' you can check of the user has a session running. logout or 4 hours of idle will the session be deleted.
*/

class cls_sessie
{
	public function add($_idklant, $_ip, $_date, $_time)
	{
		if($_idklant != '' || $_ip != '' || $_date != '' || $_time != '')
		{
			mysql_query("INSERT INTO sessie (idklant, ip, date, timestamp)
			VALUES('".$_idklant."', '".$_ip."', '".$_date."', '".$_time."')");
		} else {
			echo "Error, check input.";
		}
	}
	
	public function delete($_id) 
	{
		if($_id != '')
		{
			mysql_query("DELETE FROM sessie WHERE id = '".$_id."'");
		} else {
			echo "Error, check input.";
		}
	}
	
	public function check($_ip, $_idklant) 
	{
		if($_ip != '' || $_klantid != '')
		{
			$result = mysql_query("SELECT id, idklant, ip FROM sessie WHERE ip = '".$_ip."' AND idklant = '".$_idklant."'");
			
			if(mysql_num_rows($result)>0)
			{
				return 1;
			} else {
				return 0;
			}
		} else {
			echo "Error, check input.";
		}
	}
	
	
	public function diffTime($bigTime,$smallTime)
	{
        list($h1,$m1,$s1)=split(":",$bigTime);
        list($h2,$m2,$s2)=split(":",$smallTime);
       
        $second1=$s1+($h1*3600)+($m1*60);//converting it into seconds
        $second2=$s2+($h2*3600)+($m2*60);
       
        if ($second1==$second2)
        {
            $resultTime="00:00:00";
            return $resultTime;
            exit();
        }
       
        if ($second1<$second2) //
        {
            $second1=$second1+(24*60*60);//adding 24 hours to it.
        }
       
        $second3=$second2-$second1;
       
        //print $second3;
        if ($second3==0)
        {
            $h3=0;
        }
        else
        {
            $h3=floor($second3/3600);//find total hours
        }
           
        $remSecond=$second3-($h3*3600);//get remaining seconds
        if ($remSecond==0)
        {
            $m3=0;
        }
        else
        {
            $m3=floor($remSecond/60);// for finding remaining  minutes
        }
        
        $s3=$remSecond-(60*$m3);
        
        if($h3==0)//formating result.
        {
            $h3="00";
        }
        if($m3==0)
        {
            $m3="00";
        }
        if($s3==0)
        {
            $s3="00";
        }
        
		//correctie
		$h3 = $h3 + 24;
		
        return $h3;
	}
}
?>